<template>
	<view class="content">
		<view class="top-bar">
			<!-- 绝对定位 不站位 -->
			<view class="top-bar-left" @tap="backOne">
				<image src="../../static/back.png" class="back-img"></image>
			</view>
			<view class="top-bar-center">
				<text>给谁发消息</text>
			</view>
			<view class="top-bar-right">
				<image src="../../static/yangmi.jpg" class="user-img"></image>
			</view>
		</view>
		<scroll-view class="chat" scroll-y="true" scroll-with-animation="true" :scroll-into-view="scrollToView">
			<view class="chat-main" v-bind:style="{paddingBottom: chatheight + 'px'}">
				<view class="chat-ls" v-for="(item,index) in msgList" :key="index" :id="'msg' + item.msgId">
					<view class="chat-time" v-if="item.time != ''">{{dateTimeUtils(item.time)}}</view>
					<view class="msg-m msg-left" v-if="item.id != 'a'">
						<image :src="item.icon" class="user-img"></image>
						<view class="message" v-if="item.type == 0">
							<view class="msg-text">
								{{item.message}}
							</view>
						</view>
						<view class="message" v-if="item.type == 1">
							<image :src="item.message" class="msg-img" mode="widthFix"
								@tap="preViewImage(item.message)"></image>
						</view>
					</view>
					<view class="msg-m msg-right" v-if="item.id == 'a'">
						<image :src="item.icon" class="user-img"></image>
						<view class="message" v-if="item.type == 0">
							<view class="msg-text">
								{{item.message}}
							</view>
						</view>
						<view class="message" v-if="item.type == 1">
							<image :src="item.message" class="msg-img" mode="widthFix"
								@tap="preViewImage(item.message)"></image>
						</view>
					</view>
				</view>
			</view>
			<!-- <view class="padbar"></view> -->
		</scroll-view>
		<bottombar @inputs="inputs" @heights="heights"></bottombar>
	</view>
</template>

<script>
	// TODO  scrollToView 无法滚动到最底部
	import datas from "../../common/js-data/test_data.js"
	import dateTimeUtils from "../../utils/dateTimeUtils.js"
	import bottombar from '../../components/imbottombar/imbottombar.vue'

	export default {
		components: {
			bottombar
		},
		data() {
			return {
				msgList: [],
				imageList: [],
				oldTime: new Date(),
				scrollToView: '',
				// chat-main高度，不能写死，要动态获取适配机型
				chatheight: 46
			}
		},
		onLoad: function() {
			this.getMsgLst();
		},
		methods: {
			// 滚动到底部
			goBottom: function() {
				this.scrollToView = '';
				this.$nextTick(function() {
					let len = this.msgList.length - 1
					this.scrollToView = 'msg' + this.msgList[len].msgId
				})
			},
			// 设置imbottom高度
			heights: function(e) {
				this.chatheight = e
				console.log('高度', this.chatheight)
				this.goBottom()
			},
			inputs: function(e) {
				console.log(e)
				let len = this.msgList.length
				// 发送消息
				let msg = {
					id: 'a',
					icon: '../../static/yangmi.jpg',
					message: e.message,
					type: e.type,
					time: '2021-05-26 15:11:21',
					state: 0,
					msgId: len + 1
				}
				this.msgList.push(msg)
				// 滚动到底部
				this.$nextTick(function() {
					this.scrollToView = 'msg' + this.msgList[this.msgList.length - 1].msgId
				})

				// 接收消息
			},
			dateTimeUtils: function(dateTime) {
				return dateTimeUtils.msgDateTime(dateTime)
			},
			backOne: function() {
				uni.navigateBack({
					delta: 1
				})
			},
			// 获取消息
			getMsgLst: function() {
				let msgList = datas.getMsgList()
				// 保存图片，提供预览
				for (var i = 0; i < msgList.length; i++) {
					// 最后一条必须显示时间
					if (i < msgList.length - 1) {
						// 时间
						let t = dateTimeUtils.spaceTime(this.oldTime, msgList[i].time);
						if (t) {
							this.oldTime = t
						}
						msgList[i].time = t
					}
					if (msgList[i].type == 1) {
						this.imageList.unshift(msgList[i].message)
					}
					this.msgList.unshift(msgList[i])
				}

				// 滚动到底部
				this.$nextTick(function() {
					this.scrollToView = 'msg' + this.msgList[i - 1].msgId
				})
			},
			//  图片预览
			preViewImage: function(e) {
				let index = 0;
				for (let i = 0; i < this.imageList.length; i++) {
					if (this.imageList[i] == e) {
						index = i
					}
				}
				// 预览图片
				uni.previewImage({
					current: index,
					urls: this.imageList,
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
	}

	.chat {
		height: 100%;

		.chat-main {
			padding-left: 16rpx;
			padding-right: 16rpx;
			padding-top: 100rpx;
			display: flex;
			flex-direction: column;
		}

		.chat-ls {
			padding-bottom: 10px;

			.chat-time {
				font-size: $uni-font-size-sm;
				color: rgba(39, 40, 50, 0.3);
				line-height: 44rpx;
				padding: 20rpx 9;
				text-align: center;
			}

			.msg-m {
				display: flex;
				padding: 15rpx 0;

				.user-img {
					flex: none;
					height: 80rpx;
					width: 80rpx;
					border-radius: 20rpx;
				}

				.message {
					flex: none;
					max-width: 480rpx;
				}

				.msg-text {
					font-weight: 400;
					color: $uni-text-color;
					line-height: 44rpx;
					padding: 18rpx 24rpx;
				}

				.msg-img {
					max-width: 460rpx;
					border-radius: $uni-border-radius-lg;
					// max-height: 560rpx;
				}
			}

			.msg-left {
				flex-direction: row;

				.msg-text {
					margin-left: 16rpx;
					background-color: #FFFFFF;
					border-radius: 0 20rpx 20rpx 20rpx;
				}

				.msg-img {
					margin-left: 16rpx;
				}
			}

			.msg-right {
				flex-direction: row-reverse;

				.msg-text {
					margin-right: 16rpx;
					background-color: rgba(255, 228, 49, 1);
					border-radius: 20rpx 0rpx 20rpx 20rpx;
				}

				.msg-img {
					margin-right: 16rpx;
				}
			}
		}
	}

	.content {
		background: rgba(244, 244, 244, 0.96);
		height: 100%;
		position: relative;
	}

	.top-bar {
		// 顶部高度
		padding-top: var(--status-bar-height);
		z-index: 1000;
		position: absolute;
		background: rgba(244, 244, 244, 1);
		border-bottom: 1px solid $uni-border-color;
		height: 90rpx;
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;

		image {
			height: 68rpx;
			width: 68rpx;
		}

		.top-bar-left {
			padding-left: 15rpx;
		}

		.top-bar-right {
			padding-right: 15rpx;

			image {
				border-radius: 16rpx;
			}
		}
	}
</style>
